﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using IndustrialSmokeCalc.SmokeCalcWCF;
using Telerik.Windows.Controls;

namespace IndustrialSmokeCalc
{
    public partial class MeasurementDetail : PhoneApplicationPage
    {
        public MeasurementDetail()
        {
            InitializeComponent();
            busyIndicator.IsRunning = true;
            LoadMeasurementDetail();
        }

        private void LoadMeasurementDetail()
        {
            SmokeCalcClient client = new SmokeCalcClient();
            client.GetMeasurementListCompleted += GetMeasurementList;
            client.GetMeasurementListAsync("id", DataSingleton.Instance.MeasurementId);
        }

        private void GetMeasurementList(object sender, GetMeasurementListCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show("Tidak dapat melakukan koneksi ke server.\r\nCoba periksa koneksi internet anda.");
                NavigationService.Navigate(new Uri("/History.xaml", UriKind.Relative));
            }
            else if (e.Result != null)
            {
                btnViewChart.Visibility = Visibility.Visible;

                Measurement m = e.Result[0];
                txtObserver.Text = (!m.MeasurementObserver.IsEmpty()) ? m.MeasurementObserver : "-";
                txtDate.Text = (!m.MeasurementDate.IsEmpty()) ? m.MeasurementDate : "-";
                txtTime1.Text = (!m.MeasurementTime1.IsEmpty()) ? m.MeasurementTime1 : "-";
                txtTime2.Text = (!m.MeasurementTime2.IsEmpty()) ? m.MeasurementTime2 : "-";
                txtResultRingelmann.Text = (m.MeasurementResultRingelmann != "0") ? m.MeasurementResultRingelmann : "-";
                txtUnitAddr.Text = (!m.MeasurementUnitAddr.IsEmpty()) ? m.MeasurementUnitAddr : "-";
                txtEmissionAddr.Text = (!m.MeasurementEmissionAddr.IsEmpty()) ? m.MeasurementEmissionAddr : "-";
                txtHeight.Text = (m.MeasurementHeight != "0") ? m.MeasurementHeight : "-";
                txtRelativeHeight.Text = (m.MeasurementRelativeHeight != "0") ? m.MeasurementRelativeHeight : "-";
                txtDistance.Text = (m.MeasurementDistance != "0") ? m.MeasurementDistance : "-";
                txtDir1.Text = (!m.MeasurementDir1.IsEmpty()) ? m.MeasurementDir1 : "-";
                txtDir2.Text = (!m.MeasurementDir2.IsEmpty()) ? m.MeasurementDir2 : "-";
                txtEmissionDes1.Text = (!m.MeasurementEmissionDes1.IsEmpty()) ? m.MeasurementEmissionDes1 : "-";
                txtEmissionDes2.Text = (!m.MeasurementEmissionDes2.IsEmpty()) ? m.MeasurementEmissionDes2 : "-";
                txtEmissionColor1.Text = (!m.MeasurementEmissionColor1.IsEmpty()) ? m.MeasurementEmissionColor1 : "-";
                txtEmissionColor2.Text = (!m.MeasurementEmissionColor2.IsEmpty()) ? m.MeasurementEmissionColor2 : "-";
                txtPoint1.Text = (!m.MeasurementPoint1.IsEmpty()) ? m.MeasurementPoint1 : "-";
                txtPoint2.Text = (!m.MeasurementPoint2.IsEmpty()) ? m.MeasurementPoint2 : "-";
                txtBgDes1.Text = (!m.MeasurementBgDes1.IsEmpty()) ? m.MeasurementBgDes1 : "-";
                txtBgDes2.Text = (!m.MeasurementBgDes2.IsEmpty()) ? m.MeasurementBgDes2 : "-";
                txtBgColor1.Text = (!m.MeasurementBgColor1.IsEmpty()) ? m.MeasurementBgColor1 : "-";
                txtBgColor2.Text = (!m.MeasurementBgColor2.IsEmpty()) ? m.MeasurementBgColor2 : "-";
                txtSky1.Text = (!m.MeasurementSky1.IsEmpty()) ? m.MeasurementSky1 : "-";
                txtSky2.Text = (!m.MeasurementSky2.IsEmpty()) ? m.MeasurementSky2 : "-";
                txtWind1.Text = (m.MeasurementWind1 != "0") ? m.MeasurementWind1 : "-";
                txtWind2.Text = (m.MeasurementWind2 != "0") ? m.MeasurementWind2 : "-";
                txtWindDir1.Text = (!m.MeasurementWindDir1.IsEmpty()) ? m.MeasurementWindDir1 : "-";
                txtWindDir2.Text = (!m.MeasurementWindDir2.IsEmpty()) ? m.MeasurementWindDir2 : "-";
                txtTemp1.Text = (m.MeasurementTemp1 != "0") ? m.MeasurementTemp1 : "-";
                txtTemp2.Text = (m.MeasurementTemp2 != "0") ? m.MeasurementTemp2 : "-";
                txtWetTemp.Text = (m.MeasurementWetTemp != "0") ? m.MeasurementWetTemp : "-";
                txtRh.Text = (!m.MeasurementRh.IsEmpty()) ? m.MeasurementRh : "-";

                DataSingleton.Instance.StringOpacity = m.MeasurementResultDetail;
                DataSingleton.Instance.StartTime = m.MeasurementTime1;

                busyIndicator.IsRunning = false;
                Thread.Sleep(500);
                stackPanel.Visibility = Visibility.Visible;
            }
        }

        private void btnViewChart_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/MeasurementDetailChart.xaml", UriKind.Relative));
        }
    }
}